package com.zr.lads.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.zr.lads.dao.FuncDao;
import com.zr.lads.dao.UserDao;
import com.zr.lads.pojo.Func;
import com.zr.lads.pojo.User;
import com.zr.lads.service.FuncService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 功能服务实现类
 * @author tang
 *
 */
@Repository("funcService")
public class FuncServiceImpl implements FuncService {

	@Resource
	private FuncDao<Func> funcDao;
	@Resource
	private UserDao<User> userDao;
	
	@Override
	public String getCurrentUserFuncs(int uid) {
		String funcs = null;
		User user = userDao.get(User.class, uid);
		funcs = user.getRole().getRoleFuncs();
		return funcs;
	}

	@Override
	public JSONArray getFuncsByParentId(int parentId, String sfunc) {
		JSONArray funcs = new JSONArray();
		StringBuffer hql = new StringBuffer("from Func f where f.funcParentId=? and f.funcId in(");
		hql.append(sfunc);
		hql.append(")");
		Object[] param = new Object[]{parentId};
		List<Func> list = funcDao.find(hql.toString(),param);
		for (Func func : list) {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("id", func.getFuncId());
			jsonObject.put("text", func.getFuncName());
			jsonObject.put("state", func.getFuncState());
			JSONObject attr = new JSONObject();
			attr.put("fpath", func.getFuncPath());
			jsonObject.put("attributes", attr);
			funcs.add(jsonObject);
		}
		return funcs;
	}

	@Override
	public JSONArray getCurrentUserFuncsByParentId(int parentId, String sfunc) {
		JSONArray userFuncs = this.getFuncsByParentId(parentId, sfunc);
		for (int i = 0; i < userFuncs.size(); i++) {
			String state = userFuncs.getJSONObject(i).getString("state");
			if ("open".equals(state)){
				continue;
			} else {
				int currentId = userFuncs.getJSONObject(i).getInt("id");
				JSONArray children = this.getCurrentUserFuncsByParentId(currentId, sfunc);
				userFuncs.getJSONObject(i).put("children", children);
			}
		}
		return userFuncs;
	}

	@Override
	public JSONArray getFuncsByParentId(int parentId, String allFunc, String sfunc) {
		JSONArray funcs = new JSONArray();
		StringBuffer hql = new StringBuffer("from Func f where f.funcParentId=? and f.funcId in(");
		hql.append(allFunc);
		hql.append(")");
		Object[] param = new Object[]{parentId};
		List<Func> list = funcDao.find(hql.toString(),param);
		for (Func func : list) {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("id", func.getFuncId());
			jsonObject.put("text", func.getFuncName());
			jsonObject.put("state", func.getFuncState());
			if (sfunc.contains(String.valueOf(func.getFuncId())) && "open".equals(func.getFuncState())) {
				jsonObject.put("checked", true);
			}
			JSONObject attr = new JSONObject();
			attr.put("fpath", func.getFuncPath());
			jsonObject.put("attributes", attr);
			funcs.add(jsonObject);
		}
		return funcs;
	}

	@Override
	public JSONArray getCurrentUserFuncsByParentId(int parentId, String allFunc, String sfunc) {
		JSONArray userFuncs = this.getFuncsByParentId(parentId, allFunc, sfunc);
		for (int i = 0; i < userFuncs.size(); i++) {
			String state = userFuncs.getJSONObject(i).getString("state");
			if ("open".equals(state)){
				continue;
			} else {
				int currentId = userFuncs.getJSONObject(i).getInt("id");
				JSONArray children = this.getCurrentUserFuncsByParentId(currentId, allFunc, sfunc);
				userFuncs.getJSONObject(i).put("children", children);
			}
		}
		return userFuncs;
	}

	@Override
	public String getAllFuncIds() {
		StringBuffer sb = new StringBuffer();
		List<Func> funcs = funcDao.getAllFuncs();
		for (int i = 0; i < funcs.size(); i++) {
			if (i == 0) {
				sb.append(funcs.get(i).getFuncId());
			} else {
				sb.append(",");
				sb.append(funcs.get(i).getFuncId());
			}
		}
		return sb.toString();
	}

}
